package com.common;


import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;


/**
 * @autor: ZHANG XIAOYU
 * @date�� 2019-05-06  16:24
 */
class ToInterface {

    public static String httpPost(String urlStr){
        URL connect;
        StringBuffer data = new StringBuffer();
        String s = "[{\"unitName\":11}]";
        try {
            connect = new URL(urlStr);
            HttpsURLConnection connection = (HttpsURLConnection)connect.openConnection();

            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
            connection.setRequestProperty("X-AUTH",
                    "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjEsInNzb1Rva2VuIjoiUHhlbEpBc3Z0MW5saXJNaiJ9.ji71PNnNkAlg0nYI1lUvlwHYbm8a5nbPO4b6RcvJggelw70U4d9f7DLSqSvUzDuX97jafdGxI2acOrs46zLrcA");

            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            /*
            JSONArray json = new JSONArray();
            json.put(new JSONObject().put("unitName",11));
            */
            out.writeBytes(s);
            out.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                data.append(line);
            }

            out.close();
            reader.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data.toString();
    }

    public static void main(String[] args){

        String url="https://api2-test.blacklake.cn/schedule_for_yuanli/v1/rest/unit";

        System.out.println(httpPost(url));

    }

}
